package com.wimcorp.magic.spell;

import java.util.ArrayList;
import java.util.List;

import com.wimcorp.magic.abilities.Ability;
import com.wimcorp.magic.behaviour.Behaviour;
import com.wimcorp.magic.cards.Artifact;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.cards.Enchantment;
import com.wimcorp.magic.cards.Instant;
import com.wimcorp.magic.cards.Sorcery;

//@Embeddable
public class SpellHolder {

	//@ManyToOne
	private Creature creature;

	//@ManyToMany(cascade = CascadeType.ALL)
	//@JoinTable(name = "playingcard_enchantment", joinColumns = @JoinColumn(name = "playingcard_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "enchantment_id", referencedColumnName = "id"))
	private List<Enchantment> enchantments = new ArrayList<Enchantment>();
	
	private List<Sorcery> sorceries = new ArrayList<Sorcery>();
	//@ManyToMany(cascade = CascadeType.ALL)
	//@JoinTable(name = "playingcard_instant", joinColumns = @JoinColumn(name = "playingcard_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "instant_id", referencedColumnName = "id"))
	private List<Instant> instants = new ArrayList<Instant>();
	

	//@ManyToMany(cascade = CascadeType.ALL)
	//@JoinTable(name = "playingcard_artifact", joinColumns = @JoinColumn(name = "playingcard_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "artifact_id", referencedColumnName = "id"))
	private List<Artifact> artifacts = new ArrayList<Artifact>();

	protected SpellHolder(){}
	
	public SpellHolder(Creature creature) {
		this.creature = creature;
	}

	public void addEnchantment(Enchantment enchantment) {
		enchantments.add(enchantment);
		//for (Behaviour behaviour : enchantment.getBehaviour()) {
		//	creature.addBehaviour(behaviour);
		//}
	}

	public void removeEnchantment(Enchantment enchantment) {
		enchantments.remove(enchantment);
		//for (Behaviour behaviour : enchantment.getBehaviour()) {
		//	creature.removeBehaviour(behaviour);
		//}
		for(Ability ability : enchantment.getAbilities()){
			ability.uncast();
		}
	}

	public List<Enchantment> getEnchantments() {
		return enchantments;
	}

	public boolean hasEnchantment(Enchantment enchantment) {
		return enchantments.contains(enchantment);
	}
	
	public void addSorcery(Sorcery sorcery) {
		sorceries.add(sorcery);
	}

	public void removeSorcery(Sorcery sorcery) {
		sorceries.remove(sorcery);
		for(Ability ability : sorcery.getAbilities()){
			ability.uncast();
		}
	}

	public List<Sorcery> getSorceries() {
		return getSorceries();
	}

	public boolean hasSorcery(Sorcery sorcery) {
		return sorceries.contains(sorcery);
	}

	public void addInstant(Instant instant) {
		instants.add(instant);
		//for (Behaviour behaviour : instant.getBehaviour()) {
		//	creature.addBehaviour(behaviour);
		//}
		
	}

	public void removeInstant(Instant instant) {
		instants.remove(instant);
		//for (Behaviour behaviour : instant.getBehaviour()) {
		//	creature.removeBehaviour(behaviour);
		//}
		for(Ability ability : instant.getAbilities()){
			ability.uncast();
		}
	}

	public List<Instant> getInstants() {
		return instants;
	}

	public boolean hasInstant(Instant instant) {
		return instants.contains(instant);
	}

	public void addArtifact(Artifact artifact) {
		artifacts.add(artifact);
		//for (Behaviour behaviour : artifact.getBehaviour()) {
		//	creature.addBehaviour(behaviour);
		//}
		
	}

	public void removeArtifact(Artifact artifact) {
		artifacts.remove(artifact);
		//for (Behaviour behaviour : artifact.getBehaviour()) {
		//	creature.removeBehaviour(behaviour);
		//}
		for(Ability ability : artifact.getAbilities()){
			ability.uncast();
		}
	}

	public List<Artifact> getArtifacts() {
		return artifacts;
	}

	public boolean hasInstant(Artifact artifact) {
		return artifacts.contains(artifact);
	}

	public void graveyard() {
		List<Enchantment> enchantments2 = new ArrayList<Enchantment>(enchantments);
		for (Enchantment enchantment : enchantments2) {
			removeEnchantment(enchantment);
			enchantment.graveyard();
		}
		List<Sorcery> sorceries2 = new ArrayList<Sorcery>(sorceries);
		for(Sorcery sorcery : sorceries2){
			removeSorcery(sorcery);
			//sorcery.graveyard();
		}
		List<Instant> instants2 = new ArrayList<Instant>(instants);
		for (Instant instant : instants2) {
			removeInstant(instant);
			//instant.graveyard();
		}
		List<Artifact> artifacts2 = new ArrayList<Artifact>(artifacts);
		for(Artifact artifact : artifacts2){
			removeArtifact(artifact);
		}
	}

	/*public void exile() {
		for (Enchantment enchantment : enchantments) {
			removeEnchantment(enchantment);
			enchantment.exile();
		}
		for (Instant instant : instants) {
			removeInstant(instant);
			instant.exile();
		}
		for(Artifact artifact : artifacts){
			removeArtifact(artifact);
		}
	}*/

	public String toString() {
		StringBuilder spellHolder = new StringBuilder();
		for (Enchantment enchantment : enchantments) {
			spellHolder.append(enchantment);
		}
		for (Artifact artifact : artifacts) {
			spellHolder.append(artifact);
		}
		for (Sorcery sorcery : sorceries) {
			spellHolder.append(sorcery);
		}
		for (Instant instant : instants) {
			spellHolder.append(instant);
		}
		return spellHolder.toString();
	}

	public int size() {
		return enchantments.size() + instants.size() + sorceries.size() + artifacts.size();
	}



}
